//
//  HLiveInputView.m
//  linktuIOS
//
//  Created by hejunsong on 2017/1/14.
//  Copyright © 2017年 tianxie. All rights reserved.
//

#import "HLiveInputView.h"
#import "Masonry.h"
#import "HMessageTool.h"
#import "HBalanceViewModel.h"


#define  Snedbutllt  [NSString stringWithFormat:@"%@Wallet/barrageCharge",apihttpWallet]
@interface HLiveInputView()<UITextFieldDelegate>

@property(nonatomic,strong) UIButton *switchButton;

@property(nonatomic,strong) UITextField *inputtextField;

@property(nonatomic,strong) UIView *inputbgView;

@property(nonatomic,strong) NSAttributedString *placeHolderStr;

@property(nonatomic,strong) NSAttributedString *noramlplaceHolderStr;

@property(nonatomic,strong) NSAttributedString *placeHolderblackStr;


@end

@implementation HLiveInputView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _pay_barrage_amount = 1;
        [self makeUI];
    }
    return self;
}



-(void)makeUI
{
    _switchButton = [UIButton new];
    _inputtextField = [UITextField new];
    _inputtextField.returnKeyType = UIReturnKeySend;
    _inputtextField.textColor = [UIColor blackColor];
    _inputtextField.delegate = self;
    _inputbgView = [UIView new];
    
    [_switchButton setImage:[UIImage imageNamed:@"Barrage-off"]  forState:UIControlStateNormal];
    [_switchButton setImage:[UIImage imageNamed:@"Barrage-open"] forState:UIControlStateSelected];
    [_switchButton addTarget:self action:@selector(butlltSwitch:) forControlEvents:UIControlEventTouchUpInside];
    
    _inputbgView.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.9];
    [self addSubview:_inputbgView];
    
    self.backgroundColor = [UIColor clearColor];
    
    [self addSubview:_switchButton];
   
    
    
    NSString *tempstr = [NSString stringWithFormat:NSLTLocalizedString(@"发送弹幕，%ld钻石／条", nil)
,_pay_barrage_amount];
    NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:tempstr attributes:
                                      @{NSForegroundColorAttributeName:[UIColor lightGrayColor],
                                        NSFontAttributeName:[UIFont systemFontOfSize:14]
                                        }];
    
    self.noramlplaceHolderStr = [[NSAttributedString alloc] initWithString:@"和大家说点什么" attributes:
                                      @{NSForegroundColorAttributeName:[UIColor lightGrayColor],
                                        NSFontAttributeName:[UIFont systemFontOfSize:14]
                                        }];
    
    
    self.placeHolderStr = attrString;
    self.placeHolderblackStr =  [[NSAttributedString alloc] initWithString:NSLTLocalizedString(@"您已被禁言", nil) attributes:
                                 @{NSForegroundColorAttributeName:[UIColor lightGrayColor],
                                   NSFontAttributeName:[UIFont systemFontOfSize:14]
                                   }];
    
    self.inputtextField.attributedPlaceholder = self.noramlplaceHolderStr;
    
    [_inputbgView addSubview:_inputtextField];
    
    [_switchButton mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.mas_equalTo(self.mas_left).offset(10);
        make.width.equalTo(@38);
        make.height.equalTo(@38);
        make.centerY.mas_equalTo(self.centerY).offset(0);
        
    }];
    
    [_inputbgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.mas_left).offset(0);
        make.right.mas_equalTo(self.mas_right).offset(0);
        make.bottom.mas_equalTo(self.mas_bottom).offset(0);
        make.top.mas_equalTo(self.mas_top).offset(0);
    }];
    
    [_inputtextField mas_makeConstraints:^(MASConstraintMaker *make) {
         make.left.mas_equalTo(_switchButton.mas_right).offset(4);
         make.right.mas_equalTo(self.mas_right).offset(-4);
         make.bottom.mas_equalTo(self.mas_bottom).offset(0);
         make.top.mas_equalTo(self.mas_top).offset(0);
    }];

    
}

-(void)butlltSwitch:(UIButton *)but
{
    if(_switchButton.selected)
    {
        _switchButton.selected = NO;
         self.inputtextField.attributedPlaceholder = self.noramlplaceHolderStr;
    }else
    {
        _switchButton.selected = YES;
        NSString *tempstr = @"发送弹幕，1MET／条";
        self.placeHolderStr =  [[NSAttributedString alloc] initWithString:tempstr attributes:
                                @{NSForegroundColorAttributeName:[UIColor lightGrayColor],
                                  NSFontAttributeName:[UIFont systemFontOfSize:14]
                                  }];
        self.inputtextField.attributedPlaceholder = self.placeHolderStr;
        
    }
    if(_isBlack)
    {
        self.inputtextField.attributedPlaceholder = self.placeHolderblackStr;
    }
}
-(void)setPay_barrage_amount:(NSInteger)pay_barrage_amount
{
    _pay_barrage_amount = pay_barrage_amount;
    NSString *tempstr = [NSString stringWithFormat:NSLTLocalizedString(@"发送弹幕，%ldMET／条", nil)
,pay_barrage_amount];
    self.placeHolderStr =  [[NSAttributedString alloc] initWithString:tempstr attributes:
                            @{NSForegroundColorAttributeName:[UIColor lightGrayColor],
                              NSFontAttributeName:[UIFont systemFontOfSize:14]
                              }];
    if(self.switchButton.selected){
        self.inputtextField.attributedPlaceholder = self.placeHolderStr;
    }

}


-(void)setIsBlack:(BOOL)isBlack
{
    _isBlack = isBlack;
    if(_isBlack)
    {
        self.inputtextField.attributedPlaceholder = self.placeHolderblackStr;
    }else
    {
        if(self.switchButton.selected)
        {
            self.inputtextField.attributedPlaceholder = self.placeHolderStr;
        }else
        {
            self.inputtextField.attributedPlaceholder = nil;
        }
    }
}
-(void)setIs_pay_barrage:(BOOL)is_pay_barrage
{
    _is_pay_barrage = is_pay_barrage;
}

-(void)becomeliveFristResponder
{
    [self.inputtextField becomeFirstResponder];
}
-(void)resignliveFirstResponder
{
    [self.inputtextField resignFirstResponder];
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    return  !_isBlack;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if(textField.text.length ==  0)
    {
        return YES;
    }
    if(_switchButton.selected)
    {
        NSString *text = textField.text;
        [[MTLiveApiManager manager] sendBarrageWithcompletion:^(MTError *error) {
            if(error == nil)
            {
                self.balanceViewModel.currentDiamondNumber =  self.balanceViewModel.currentDiamondNumber  - 1;
                if(self.delegate && [self.delegate respondsToSelector:@selector(hliveInputViewaddMessage:isBullet:)])
                {
                    [self.delegate hliveInputViewaddMessage:text isBullet:_switchButton.selected];
                }
            }else
            {
                [MBProgressHUD showMessage:error.errorDescription];
            }
        }];
    }else
    {
        if(self.delegate && [self.delegate respondsToSelector:@selector(hliveInputViewaddMessage:isBullet:)])
        {
            [self.delegate hliveInputViewaddMessage:textField.text isBullet:_switchButton.selected];
        }
    }
    textField.text = @"";
    return YES;
}

-(void)setAitaStr:(NSString *)aitaStr
{
    _aitaStr = aitaStr;
    self.inputtextField.text = [NSString stringWithFormat:@"@%@ ",aitaStr];
}
-(void)dealloc
{
    NSLog(@"dealloc --inputView");
    
}
@end
